package cn.itsource.pethome.base.controller;

import cn.itsource.pethome.base.domain.dto.VerifyDTO;
import cn.itsource.pethome.base.service.IImageVerifyService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class ImageVerifyCodeController {
    @Autowired
    private IImageVerifyService iImageVerifyService;
    /**
     * 生成图片验证码
     * @param key 前端传给我的唯一键
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key")String key){
        try {
            String base64code = iImageVerifyService.imageVerifyCode(key);
            return AjaxResult.me().setData(base64code);
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 发送短信验证码的接口
     * @param verifyDTO
     * @return
     */
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody VerifyDTO verifyDTO){
        try {
            iImageVerifyService.sendSmsCode(verifyDTO);
            return AjaxResult.me();
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

}
